class Hlsjs {
  constructor (player) {
    this.player = player
  }

  setup ({file}) {
    this.file = file
    this.events = [this.player.once('beforePlay', () => this.loadHls())]
  }

  loadHls () {
    this.hlsPlayer = new window.Hls()
    this.hlsPlayer.loadSource(this.file)
    this.hlsPlayer.attachMedia(this.player.dom.video)
  }

  remove () {
    if (this.hlsPlayer) {
      this.hlsPlayer.detachMedia()
      this.hlsPlayer.destroy()
    }
    if (this.events) {
      this.events.forEach(e => this.player.off(e))
    }
  }
}

Hlsjs.formats = {
  'application/vnd.apple.mpegurl': 'hls'
}

Hlsjs.canPlayType = function (type) {
  if (window.Hls.isSupported() && type in Hlsjs.formats) {
    return 'maybe'
  }

  return ''
}

Hlsjs.canPlaySource = function (srcObj, options) {
  return Hlsjs.canPlayType(srcObj.type)
}

Hlsjs.VERSION = '__VERSION__'

export default Hlsjs
